#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++) 
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-', ch = getchar(); 
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return f ? -x : x;
}

const int N = 1e3 + 10;
const int H[4][2] = { { 0, -1 }, { 0, 1 } , { 1, 0 }, { -1, 0 }};

int dis[N][N], R[N][N], U[N][N], D[N][N], L[N][N], r, c;
bool vis[N][N];
char s[N][N];

priority_queue < pair < int, pii > > q;

void upd(int x, int y, int d) { if(s[x][y] == '.' && dis[x][y] > d) dis[x][y] = d, q.push({ -dis[x][y], { x, y }}); }

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	r = in, c = in; rep(i, 1, r) scanf("%s", s[i] + 1);
	rep(i, 1, r) {
		rep(j, 1, c) L[i][j] = s[i][j] == '#' ? j : L[i][j - 1];
		per(j, c, 1) R[i][j] = s[i][j] == '#' ? j : R[i][j + 1];
	}
	rep(j, 1, c) {
		rep(i, 1, r) U[i][j] = s[i][j] == '#' ? i : U[i - 1][j];
		per(i, r, 1) D[i][j] = s[i][j] == '#' ? i : D[i + 1][j];
	}
	int sx = in, sy = in; memset(dis, 0x3f, sizeof dis); dis[sx][sy] = 0; q.push({ 0, { sx, sy }});
	int tx = in, ty = in;
	while(q.size()) {
		auto d = q.top().se; q.pop(); if(vis[d.fi][d.se]) continue; vis[d.fi][d.se] = true;
		if(d.fi == tx && d.se == ty) { printf("%d\n", dis[d.fi][d.se]); return 0; }
		rep(i, 0, 3) upd(d.fi + H[i][0], d.se + H[i][1], dis[d.fi][d.se] + 2);
		upd(d.fi, L[d.fi][d.se] + 1, dis[d.fi][d.se] + 1);
		upd(d.fi, R[d.fi][d.se] - 1, dis[d.fi][d.se] + 1);
		upd(U[d.fi][d.se] + 1, d.se, dis[d.fi][d.se] + 1);
		upd(D[d.fi][d.se] - 1, d.se, dis[d.fi][d.se] + 1);
	} puts("-1"); 
	return 0;
}
